package com.situ.dao;

import com.situ.dto.PageEntity;
import com.situ.entity.Ward;
import com.situ.util.DataSourceUtil;
import com.situ.util.StringUtil;

import java.sql.SQLException;
import java.util.List;

public class WardDao {
    public List<Ward> select(String wardNo, PageEntity page) {
        String sql = "select ward.*,department.name as 'dept_name' from ward left join department on department.id = ward.dept_id";
        if (StringUtil.isNotBlank(wardNo)) {
            sql += " where instr(wardno,'" + wardNo + "')>0";
        }
        sql += " limit ?,?";
        try {
            return DataSourceUtil.executeQuery(sql, Ward.class, (page.getPage() - 1) * page.getLimit(), page.getLimit());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer count(String wardNo) {
        String sql = "select count(1) from ward ";
        if (StringUtil.isNotBlank(wardNo)) {
            sql += " where instr(wardno,'" + wardNo + "')>0";
        }
        try {
            return DataSourceUtil.executeCount(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    public void insert(Ward ward) {
        String sql = "insert into ward(wardno,address,dept_id) values(?,?,?)";

        try {
            DataSourceUtil.executeUpdate(sql, ward.getWardno(), ward.getAddress(), ward.getDeptId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void update(Ward ward) {

        String sql = "update ward set wardno=?,address=?,dept_id=? where id = ?";
        try {
            DataSourceUtil.executeUpdate(sql, ward.getWardno(), ward.getAddress(), ward.getDeptId(), ward.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public Ward selectById(Integer id) {
        String sql = "select * from ward where id = ?";
        try {
            List<Ward> list = DataSourceUtil.executeQuery(sql, Ward.class, id);
            if (!list.isEmpty()) {
                return list.get(0);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void deleteById(Integer id) {
        String sql = "delete from ward where id = ?";
        try {
            DataSourceUtil.executeUpdate(sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
